package xginxnote

import (
	"encoding/json"

	"gitee.com/xiaoyutab/xgotool/optional/xnote"
	"gitee.com/xiaoyutab/xgotool/xgin"
	"gitee.com/xiaoyutab/xgotool/xstring"
	"github.com/gin-gonic/gin"
)

// 存储详情数据
//
//	POST	/create.content
func createContent(c *gin.Context) *xgin.Response {
	type fid_struct struct {
		Id      json.Number `form:"id" json:"id"`           // 笔记ID
		Content string      `form:"content" json:"content"` //笔记内容
	}
	temp := fid_struct{}
	if err := xgin.Bind(c, &temp); err != nil {
		return xgin.Resp().Error(err)
	}
	cont := xnote.NoteContent{
		Id:      xstring.JUint(temp.Id),
		Content: temp.Content,
	}
	// 获取详情进行权限检测
	info, err := xnote.GetMenuInfo(cont.Id)
	if err != nil {
		return xgin.Resp().Error(err)
	}
	if info.UserId != c.GetUint("uid") || info.Id != cont.Id {
		return xgin.Resp().ErrorEmpty()
	}
	// 进行数据存储
	err = xnote.SaveContent(cont.Id, cont.Content)
	if err != nil {
		return xgin.Resp().Error(err)
	}
	return xgin.Resp().SuccessEmpty()
}
